package com.qingpeng.pay.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.qingpeng.pay.R;
import com.qingpeng.pay.adapter.OrderAdapter;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseFragment;
import com.qingpeng.pay.beans.OrderBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OrderFragment extends BaseFragment {
    @BindView(R.id.title)
    TitleBar title;
    @BindView(R.id.day_shou)
    TextView dayShou;
    @BindView(R.id.day_ok)
    TextView dayOk;
    private OrderBean.DataProductBean mData;
    private List<OrderBean.DataProductBean.ListProductBean> mList;
    private boolean isCreated;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.recycler;
    }

    @Override
    public void onResume() {
        showData();
        super.onResume();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreated=true;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isCreated){
            return;
        }else {
            //进行刷新操作
            showData();
        }
    }

    private void showData() {
        Call<OrderBean> getorderlist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).getorderlist();
        getorderlist.enqueue(new Callback<OrderBean>() {
            @Override
            public void onResponse(Call<OrderBean> call, Response<OrderBean> response) {
                OrderBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(getActivity(), body.getInfo());
                    return;
                }
                mData = body.getData();
//                "<font color='#FF0000'>" + str4 + "</font>"
                dayShou.setText(Html.fromHtml("今日收单:   "+"<font color='#ff7115'>"+mData.getRecmoney() + "</font>元"));
                dayOk.setText(Html.fromHtml("今日成功:   "+"<font color='#ff7115'>"+mData.getSucmoney() + "</font>元"));
                mList = mData.getList();
                initAdapter();
            }

            @Override
            public void onFailure(Call<OrderBean> call, Throwable t) {

            }
        });
    }

    @Override
    protected void initAdapter() {
        mAdapter = new OrderAdapter(R.layout.item_order);
        mAdapter.setNewData(mList);
        super.initAdapter();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
